﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace DigiCardDeck
{
    public partial class Delete : System.Web.UI.Page
    {
        private string stConnectionString = string.Empty;
        private const string DECK_DB = "deckdb";
        private string deckNo = string.Empty;
        private string id = string.Empty;
        private string deckName = string.Empty;

        protected void Page_Load(object sender, EventArgs e)
        {

            deckNo = Request.QueryString["deckNo"];
            id = Request.QueryString["value"];
            deckName = Request.QueryString["deckName"];

            // 接続文字列を生成する
            stConnectionString += "Data Source         = " + CodeConst.DATA_SOURCE + ";";
            stConnectionString += "Initial Catalog     = " + CodeConst.INITIAL_CATALOG + ";";
            //stConnectionString += "Integrated Security = SSPI;";
            stConnectionString += "User ID             = " + CodeConst.USER_ID + ";";
            stConnectionString += "Password            = " + CodeConst.PASSWORD + ";";


            TextBox1.TextMode = TextBoxMode.Password;
            Label1.Text = id;
            Label2.Text = deckName;
        }

        protected void Button1_Click(object sender, EventArgs e)
        {

            bool deleteFlg = false;

            // SqlConnection の新しいインスタンスを生成する (接続文字列を指定)
            System.Data.SqlClient.SqlConnection cSqlConnection = (
                new System.Data.SqlClient.SqlConnection(stConnectionString)
            );

            try
            {

                // データベース接続を開く
                cSqlConnection.Open();

                // cSqlConnection から SqlCommand のインスタンスを生成する
                System.Data.SqlClient.SqlCommand hCommand = cSqlConnection.CreateCommand();

                hCommand.CommandText = "select * from deckdb where ID = '" + id + "' and  deckno = " + deckNo;

                // 指定した SQL コマンドを実行する
                System.Data.SqlClient.SqlDataReader cReader = hCommand.ExecuteReader();


                while (cReader.Read())
                {
                    if( TextBox1.Text.Equals( cReader["Password"].ToString() ) )
                    {
                        deleteFlg = true;
                    }

                }

                cReader.Dispose();


                if (deleteFlg)
                {
                    System.Data.SqlClient.SqlCommand hCommand2 = cSqlConnection.CreateCommand();

                    hCommand2.CommandText = "delete from deckdb where ID = '" + id + "' and  deckno = " + deckNo;

                    
                    hCommand2.ExecuteNonQuery();

                    
                    System.Data.SqlClient.SqlCommand hCommand3 = cSqlConnection.CreateCommand();

                    hCommand3.CommandText = "select * from deckdb where ID = '" + id + "' and  deckno > " + deckNo;


                    // 指定した SQL コマンドを実行する
                    System.Data.SqlClient.SqlDataReader cReader2 = hCommand3.ExecuteReader();

                    List<string> idList = new List<string>();

                    string updateDeckNo = string.Empty;
                    string updateId = string.Empty;
                    string updateDeckNo2 = string.Empty;

                    while (cReader2.Read())
                    {
                        
                        updateDeckNo = ((int)cReader2["deckNo"] - 1).ToString();
                        updateId = cReader2["id"].ToString();
                        updateDeckNo2 = cReader2["deckNo"].ToString();

                        idList.Add( "update deckdb set deckno = " + updateDeckNo + " where ID = '" + updateId + "' and  deckno = " + updateDeckNo2 );
                        
                    }

                    cReader2.Dispose();

                    System.Data.SqlClient.SqlCommand hCommand4 = cSqlConnection.CreateCommand();

                    foreach (var item in idList)
                    {
                        hCommand4.CommandText = item;
                        hCommand4.ExecuteNonQuery();
                    }


                    hCommand2.Dispose();

                    hCommand3.Dispose();

                    hCommand4.Dispose();
                }

                hCommand.Dispose();

                
            }
            catch (Exception ex)
            {
                Label1.Text += ex.StackTrace + " : " + ex.ToString();
            }
            finally
            {
                // データベース接続を閉じる
                cSqlConnection.Close();
                cSqlConnection.Dispose();


                if (deleteFlg)
                {
                    Response.Redirect("List.aspx");
                }
            }
        }
    }
}